
package com.maven.front.service.frontSubject;

import com.maven.front.bean.EduComment;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.mapper.EduCommentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: web-front
 * @description: 前台课程评论
 * @author: WangYiLong
 * @create: 2020-08-13 17:36
 **/

@Service
@Slf4j
public class SubjectCommentService {

    @Resource
    private EduCommentMapper eduCommentMapper;

    @Resource
    private SubjectService subjectService;


/**
     * 添加子评论
     * @param eduComment
     * @return
     */

    public int insert(EduComment eduComment) {

        int num = eduCommentMapper.insertSelective(eduComment);
        log.info("*******添加子评论Id:"+eduComment.getCommentId());
        if (num > 0) {
            //父评论回复数量加一
            EduComment parentComent = eduCommentMapper.selectByPrimaryKey(eduComment.getpCommentId());
            parentComent.setReplyCount(parentComent.getReplyCount() + 1);
            int num2 = eduCommentMapper.updateByPrimaryKeySelective(parentComent);
            return num2;
        }
        return num;
    }


/**
     * 添加父评论
     * @param eduComment
     * @return
     */

    public int insertParent(EduComment eduComment){
        return eduCommentMapper.insertSelective(eduComment);
    }


/**
     * 点赞
     * @param commentId
     * @return
     */

    public List<EduCommentDto> addPraise(Integer commentId){
        EduComment comment = eduCommentMapper.selectByPrimaryKey(commentId);
        comment.setPraiseCount(comment.getPraiseCount()+1);
        log.info("******"+comment.toString());
        int num = eduCommentMapper.updateByPrimaryKeySelective(comment);
        if(num>0){
            return subjectService.getCommentsByCourseId(comment.getOtherId());
        }else{
            return null;
        }
    }
}

